package dbmgr

import (
	"cade/src/core/db"
	"cade/src/core/log"
	"fmt"
)

// ============================================================================

type seqid_t struct {
	Id         int   `bson:"_id"`
	Seq_UserId int64 `bson:"seq_uid"`
}

// ============================================================================

func Center_CreateSeqId() {
	if DBCenter.HasCollection(C_tabname_seqid) {
		return
	}

	var obj seqid_t

	obj.Id = 1
	obj.Seq_UserId = 999999

	err := DBCenter.Insert(C_tabname_seqid, &obj)
	if err != nil {
		log.Error("dbmgr.Center_CreateSeqId() failed:", err)
	}
}

func Center_GenUserId() string {
	var obj seqid_t

	err := DBCenter.FindAndUpdate(
		C_tabname_seqid,
		db.M{"_id": 1},
		db.M{
			"$inc": db.M{"seq_uid": 1},
		},
		db.M{"seq_uid": 1},
		&obj,
	)
	if err != nil {
		log.Error("dbmgr.Center_GenUserId() failed:", err)
	}

	return fmt.Sprintf("cade-%d", obj.Seq_UserId)
}
